﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component; 

namespace web.core.Controllers
{
    public class admWebLinkController : baseController
    {
        //constructor
        #region inner function
        public List<WebLinkTypeViewModels> CreateTypeList()
        {
            var lst = new List<WebLinkTypeViewModels> { 
                createItemGroup(global_const.E_WEB_LINK.LIEN_KET, "Liên kết"),
                createItemGroup(global_const.E_WEB_LINK.TEXT, "Text link"),
                createItemGroup(global_const.E_WEB_LINK.DOI_TAC, "Đối tác"),
                createItemGroup(global_const.E_WEB_LINK.SLIDE_HOME, "Slide trang chủ"),
                createItemGroup(global_const.E_WEB_LINK.ADVS_HOME_TOP1, "Quảng cáo trang chủ - cạnh slide"),
                createItemGroup(global_const.E_WEB_LINK.ADVS_HOME_TOP2, "Quảng cáo trang chủ - dưới slide"),
                createItemGroup(global_const.E_WEB_LINK.ADVS_BOTTOM, "Quảng cáo trang chủ - bên dưới"),
                createItemGroup(global_const.E_WEB_LINK.ADVS_BOTTOM_PAGE, "Quảng cáo - bên dưới trang"),
                createItemGroup(global_const.E_WEB_LINK.CUSTOM_BLOCK, "Thông tin về chúng tôi"),
            };
            return lst;
        }
        private WebLinkTypeViewModels createItemGroup(global_const.E_WEB_LINK typeid, string typename)
        {
            var it = new WebLinkTypeViewModels()
            {
                type_id = Convert.ToString((int)typeid),
                type_name = typename
            };
            return it;
        }
        ////ddl for weblink
        //public List<SelectListItem> GetListWebLinkType(string selectedValue)
        //{
        //    var sizeDrop = new List<SelectListItem>();
        //    sizeDrop.AddRange(new SelectListItem[] { 
        //                    new SelectListItem{Selected=selectedValue==global_const.LINK_LIENKET?true:false, Text = global_const.LINK_LIENKET_DESC, Value = global_const.LINK_LIENKET },
        //                    new SelectListItem{Selected=selectedValue==global_const.LINK_TEXT?true:false, Text = global_const.LINK_TEXT_DESC, Value = global_const.LINK_TEXT },
        //                    new SelectListItem{Selected=selectedValue==global_const.LINK_DOITAC?true:false, Text = global_const.LINK_DOITAC_DESC, Value = global_const.LINK_DOITAC },
        //                    new SelectListItem{Selected=selectedValue==global_const.LINK_SLIDE_HOME?true:false, Text = global_const.LINK_SLIDE_HOME_DESC, Value = global_const.LINK_SLIDE_HOME },
        //                    new SelectListItem{Selected=selectedValue==global_const.LINK_SLIDE_ADVS_HOME?true:false, Text = global_const.LINK_SLIDE_ADVS_HOME_DESC, Value = global_const.LINK_SLIDE_ADVS_HOME }
        //                });
        //    return sizeDrop;

        //}
        #endregion
       
        //------------------------------------
        //weblink_list PAGE
        //------------------------------------
        #region weblink_list PAGE
        public ActionResult weblink_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_wl_list(int? pageindex)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            return ctr_wl_list(pageindex, admin_const.PAGE_LIST_SIZE, languageId);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ctr_wl_list(int? pageindex, int? pagesize, int? languageId)
        {
            var lstdelete = Request.Form["is_delete"];
            if (!string.IsNullOrEmpty(lstdelete)) {
                var arr = lstdelete.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(var iterator in arr){
                    if (iterator != "false")
                        weblinkModels.deleteItem(Convert.ToInt16(iterator));
                }
            } 
            var page_view = new ViewModels.WebLinkPageListViewModels()
            { 
                //list_group = accountModels.getListUserGroup(),
                list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
                pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE
            };
            TryUpdateModel(page_view);
            if (page_view.languageId != null)
            {
                languageId = page_view.languageId;
            }
            var langid = (languageId ?? admin_const.DEFAULT_LANGUAGE_ID);

            var lst_lang = languageModels.get_list_language();
            var lst_type = CreateTypeList();
            //get list type weblink
            var p_link_type = string.IsNullOrEmpty(page_view.link_type) ? Ustring.GetEnumIdToString(global_const.E_WEB_LINK.LIEN_KET) : page_view.link_type;
            page_view.select_list_type = new SelectList(lst_type, "type_id", "type_name", p_link_type);//GetListWebLinkType(p_link_type);
            page_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc");

            page_view.page_list_weblink = weblinkModels.get_plist(pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE, langid, p_link_type);
            return PartialView("../Shared/web_link/list_weblink", page_view);
        }
         
        #endregion

        //------------------------------------
        //weblink_edit PAGE
        //------------------------------------
        #region weblink_edit PAGE
        public ActionResult weblink_edit()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_wl_edit(int? id)
        {
            int itemid = id ?? 0;
            if (itemid == 0)
                ViewBag.isAdd = true;
            else
                ViewBag.isAdd = false;

            ViewModels.WebLinkViewModels item_view;
            //var item_view = new ViewModels.LanguageViewModels();
            var it = weblinkModels.getByID(itemid);
            if (it != null)
            {
                string sUrl = siteSettingSingleModels.GetConfig(global_const.WEBSITE_URL);
                item_view = new WebLinkViewModels()
                {
                    link_id = it.link_id,
                    title = it.title,
                    link = it.link,
                    type = it.type,
                    order_display = it.order_display,
                    description = it.description,
                    image_path = it.image_path,
                    language_id = it.language_id ?? admin_const.DEFAULT_LANGUAGE_ID,
                    image_display = Ufile.LoadImage(sUrl, it.image_path)

                };
            }
            else
                item_view = new WebLinkViewModels()
                {
                    type = Ustring.GetEnumIdToString(global_const.E_WEB_LINK.LIEN_KET),
                    language_id = admin_const.DEFAULT_LANGUAGE_ID,
                    order_display = admin_const.DEFAULT_ORDER_DISPLAY,
                    //listLanguage = languageModels.get_list_language()
                };

            var lst_lang = languageModels.get_list_language();
            var lst_type = CreateTypeList();
            item_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc", item_view.language_id);
            item_view.select_list_type = new SelectList(lst_type, "type_id", "type_name", item_view.type);//GetListWebLinkType(item_view.type);

            return PartialView("../Shared/web_link/edit_weblink", item_view);
        }

        [ChildActionOnly]

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult ctr_wl_edit(FormCollection collection)
        {
            var item_view = new ViewModels.WebLinkViewModels();
            TryUpdateModel(item_view);

            if (string.IsNullOrEmpty(item_view.title) )
            {
                ViewBag.result_message = "Tiêu đề không để trống.";
                ViewBag.result = false;
                return PartialView("../Shared/web_link/edit_weblink", item_view);
            }
            bool isdel = item_view.link_id != 0 ? true : false;
            var item = new data.entities.web_link()
            {
                link_id = item_view.link_id,
                title = item_view.title,
                link = item_view.link,
                type = item_view.type,
                order_display = item_view.order_display,
                description = item_view.description,
                //image_path = item_view.image_path,
                image_path = SaveImage(isdel, item_view.image_path, item_view.fileUpload, admin_const.E_IMG_TYPE.WEB_LINK, admin_const.E_IMG_THUMB.NO_THUMB),
                language_id = item_view.language_id
            };

            int ret_id = weblinkModels.weblink_update(item);

            if (ret_id > 0)
            {
                ViewBag.result_message = admin_const.UPDATE_OK;
                ViewBag.redirect = "/vi/admWebLink/weblink_list";
                return PartialView("../Shared/control_redirect");
            }
            else
            {
                ViewBag.result_message = admin_const.UPDATE_FAIL;
                ViewBag.result = false;
                return PartialView("../Shared/web_link/edit_weblink", item);
            }
        }
        #endregion
    }
}